<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <meta http-equiv="Access-Control-Allow-Origin" content="*">

  <style>
    * {
      margin: 0;
      padding: 0;
    }

    #app {
      width: 80%;
      height: 100vh;
      margin: 5px auto;
      padding-top: 50px;
    }
  </style>
</head>

<body>
  <div id="app">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <script>
    let app = new Vue({
      el: "#app",
      data() {
        var validateUsername = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入用户名'));
          }
          callback();
        };
        var validatePass = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入密码'));
          }
          callback();
        };
        return {
          ruleForm: {
            username: '',
            password: ''
          },
          rules: {
            username: [
              { validator: validateUsername, trigger: 'blur' }
            ],
            password: [
              { validator: validatePass, trigger: 'blur' }
            ]
          }
        };
      },
      methods: {
        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              // 下面那个url 是url
              axios.get('http://http://iaktim.natappfree.cc/user/loginUser', {
                params: this.ruleForm,
              }).then(res => {
                if (res.data.status  == true) {
                  this.$message({
                    message: '欢迎你，尊贵的admin',
                    type: 'success'
                  });
                  //  在这跳转
                  setTimeout(()=>{
                    window.location.href = './index.html'
                  },1000)
                } else {
                  this.$message.error('用户名密码不对');
                }
              })
            } else {
              this.$message.error('好好填');
              return false;
            }
          });
        },
        resetForm(formName) {
          this.$refs[formName].resetFields();
        }
      }
    })
  </script>
</body>

</html>